package com.ldf.media.callback;

import com.ldf.media.context.MediaServerContext;
import com.ldf.media.pool.MediaServerThreadPool;
import com.aizuda.zlm4j.callback.IMKNoReaderCallBack;
import com.aizuda.zlm4j.structure.MK_MEDIA_SOURCE;
import com.sun.jna.CallbackThreadInitializer;
import com.sun.jna.Native;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import static com.ldf.media.constants.MediaServerConstants.*;

/**
 * 无人观看回调
 *
 * @author lidaofu
 * @since 2023/11/23
 **/
@Slf4j
@Component
public class MKNoReaderCallBack implements IMKNoReaderCallBack {
    public MKNoReaderCallBack() {
        //回调使用同一个线程
        Native.setCallbackThreadInitializer(this, new CallbackThreadInitializer(true, false, "MediaNoReaderThread"));
    }

    /**
     * 某个流无人消费时触发，目的为了实现无人观看时主动断开拉流等业务逻辑
     *
     * @param sender 该MediaSource对象
     */
    public void invoke(MK_MEDIA_SOURCE sender) {
        String streamId = buildStreamId(sender);

        // 记录流暂停播放的信息，但不清除计数器
        log.info("流 {} 暂停播放", streamId);
    }

    /**
     * 构建唯一的流标识符
     */
    private String buildStreamId(MK_MEDIA_SOURCE source) {
        return String.format("%s/%s",
                MediaServerContext.ZLM_API.mk_media_source_get_app(source),
                MediaServerContext.ZLM_API.mk_media_source_get_stream(source));
    }
}
